import {
    login,
    getInfo,
    logout,
    getMenu
} from '@/api/user'
import {
    getToken,
    setToken,
    removeToken,
    setUserId,
    getUserId,
    removeUserId
} from '@/utils/auth'
import {
    resetRouter
} from '@/router'
import router from '@/router'

const getDefaultState = () => {
    return {
        token: getToken(),
        name: '',
        organizationId: '',
        userInfo: {},
        equipTypeCode: localStorage.getItem("equipTypeCode"), //当前设备类型code
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_PLATFORM_TYPE: (state, platformType) => {
        state.platformType = platformType
    },
    SET_ORGANIZATION_ID: (state, organizationId) => {
        state.organizationId = organizationId
    },
    SET_USERINFO: (state, userInfo) => {
        state.userInfo = userInfo
    },
    SET_EQUIP_TYPE_CODE: (state, equipTypeCode) => {
        state.equipTypeCode = equipTypeCode;
    },
}

const actions = {
    // user login
    login({
        commit
    }, userInfo) {
        const {
            username,
            password
        } = userInfo
        return new Promise((resolve, reject) => {
            login({
                loginname: username.trim(),
                password: password
            }).then(response => {
                const {
                    data
                } = response
                commit('SET_TOKEN', data.token)
                // commit('SET_NAME', data.username)
                setToken(data.token)
                setUserId(data.id)
                resolve(response)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    getInfo({
        commit,
        state
    }) {
        return new Promise((resolve, reject) => {
            getInfo(`/${getUserId()}`).then(response => {
                const {
                    data
                } = response
                // const { userName, avatar } = data
                commit('SET_NAME', data.userName)
                commit('SET_ORGANIZATION_ID', data.organizationId)
                commit('SET_PLATFORM_TYPE', data.type)
                commit('SET_USERINFO', data)
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getMenu({
        commit,
        state
    }) {
        return new Promise((resolve, reject) => {
            getMenu('/auth/list').then(response => {
                const {
                    data
                } = response
                if (!data) {
                    return reject('Verification failed, please Login again.')
                }
                resolve(data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // user logout
    logout({
        commit,
        state
    }) {
        removeToken() // must remove  token  first
        removeUserId();
        localStorage.clear();
        resetRouter()
        commit('RESET_STATE');
        location.reload();
        router.push(`/login`);
        // return new Promise((resolve, reject) => {
        //   logout(state.token).then((res) => {
        //     resolve(res)
        //   }).catch(error => {
        //     reject(error)
        //   })
        // })
    },

    // remove token
    resetToken({
        commit
    }) {
        return new Promise(resolve => {
            removeToken() // must remove  token  first
            removeUserId()
            commit('RESET_STATE')
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
